package com.taikang.service.impl;

import com.taikang.dao.GrabDao;
import com.taikang.dao.TaskRecordDao;
import com.taikang.entity.SpecialServiceOrder;
import com.taikang.entity.TaskRecord;
import com.taikang.service.GrabService;
import com.taikang.util.Common;
import com.taikang.util.TypeEnum;
import com.taikang.util.page.Page;
import com.taikang.util.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zhai on 16/5/10.
 */
@Service
public class GrabServiceImpl implements GrabService {

    private GrabDao grabDao;
    private TaskRecordDao taskRecordDao;

    @Autowired
    public void setTaskRecordDao(TaskRecordDao taskRecordDao) {
        this.taskRecordDao = taskRecordDao;
    }

    @Autowired
    public void setGrabDao(GrabDao grabDao) {
        this.grabDao = grabDao;
    }

    @Override
    public Pagination getGrabListByAgent(Integer agentId, Page page) {
        Pagination pagination = grabDao.getGrabListByAgent(agentId, page);
        List<SpecialServiceOrder> list = (List<SpecialServiceOrder>) pagination.getDataList();
        List<SpecialServiceOrder> result = new ArrayList<>();
        for(SpecialServiceOrder order : list) {
            int orderId = order.getOrderID();
            Map<String, Object> serviceInfo = (Map<String, Object>) grabDao.getServiceInfoByOrderId(orderId);
            if(null != serviceInfo) {
                order.setServiceName((String) serviceInfo.get("serviceName"));
                order.setPrice((String) serviceInfo.get("price"));
                order.setServiceType((String) serviceInfo.get("serviceType"));
                result.add(order);
            }
        }
        pagination.setDataList(result);
        return pagination;
    }

    @Override
    public SpecialServiceOrder getById(Integer orderId) {
        return grabDao.fetch(orderId);
    }

    @Override
    public int update(SpecialServiceOrder order) {
        TaskRecord taskRecord = new TaskRecord();
        taskRecord.setEmployeeID(Integer.parseInt(order.getEmployeeID()));
        taskRecord.setUserID(order.getUserID());
        taskRecord.setTypeID(3);
        taskRecord.setType("单次");

        Map<String, Object> map = (Map<String, Object>) grabDao.getServiceInfoByOrderId(order.getOrderID());
        TypeEnum.TodoTypeEnum todoTypeEnum = TypeEnum.TodoTypeEnum.getByValue((String) map.get("serviceType"));
        taskRecord.setTodoID(todoTypeEnum.getTodoID());
        TypeEnum.TaskStatusEnum statusEnum = TypeEnum.TaskStatusEnum.ALREADY;
        taskRecord.setStatusID(statusEnum.getStatusID());
        taskRecord.setStatus(statusEnum.getStatusName());
        taskRecord.setNumber(Integer.parseInt(Common.randNumber(8)));
        taskRecord.setPushDate(Common.dateToDateString(new Date()));
        taskRecord.setRunDate(order.getOrderDate() + " " + order.getOrderTime());
        taskRecord.setDate(order.getOrderDate() + " " + order.getOrderTime());
        taskRecord.setOk(1);
        taskRecord.setAddress(order.getAddress());
        taskRecord.setContactTel(order.getTelephone());
        taskRecord.setIntroduction(order.getName());
        taskRecord.setTarget(order.getServiceName());
        taskRecord.setOrderID(order.getOrderID());
        taskRecordDao.create(taskRecord);
        return grabDao.update(order);
    }
}
